import "../../../Utils/jquery.js";
const offset = (obj) => {
  const o = {
    left: obj.offsetLeft,
    top: obj.offsetTop,
  };
  while (obj.offsetParent) {
    obj = obj.offsetParent;
    o.left += obj.offsetLeft;
    o.top += obj.offsetTop;
  }
  return o;
};

$(function () {
  const comList = JSON.parse(sessionStorage.com || "[]");
  let $h = 0;
  $(window).on("scroll", function () {
    $h = $(window).scrollTop();
    console.log($h);
  });

  //拿到被选中的商品的信息并将它添加到缓存中去
  $(".l3-r-cont")
    .children("a")
    .on("click", function (e) {
      const com = {};
      const imgs = [];

      $(this)
        .parents(".r4-box")
        .find(".l4-cont img")
        .map((index, item) => {
          //   console.log(JSON.stringify($(item).attr("src")));
          imgs[index] = $(item).attr("src");
          //   console.log(imgs);
        });
      com.imgs = imgs;
      com.id = $(this).parents(".r4-box").find(".fl1 h5").text();
      let flag = true;
      for (let i = 0; i < comList.length; i++) {
        if (comList[i].id == com.id) {
          flag = false;
          break;
        }
      }
      const User = JSON.parse(sessionStorage.User || "[]");
      console.log(User.uname);
      if (User.uname == undefined) {
        alert("请先登录");
        return;
      }

      if (flag) {
        comList.push(com);
      }
      sessionStorage.com = JSON.stringify(comList);
      //添加动画动作
      console.log(this);
      let count = comList.length;
      $(".count").text(count);

      //飞入效果
      const { pageX, pageY } = e;

      // console.log(pageX, pageY);
      $("body").append(
        `<div class='addDiv' style='left:${pageX}px;top:${pageY}px'></div>`
      );
      // let $n = offset(".addDiv");

      $(".addDiv").animate(
        {
          width: "0px",
          height: "0px",
          left: "0px",
          top: `${150 + $h}px`,
        },
        "slow",
        function () {
          $(".addDiv").remove();
        }
      );
    });

  $(".buycar").on("click", function () {
    const User = JSON.parse(sessionStorage.User || "[]");
    if (User.uname) {
      location = "../info/info.html";
      return;
    }
    alert("请先登录");
  });
});

$(function () {
  $(document).on("scroll", function () {
    let $h = $(window).scrollTop();
    if ($h > 1000) {
      $(".calltop").css("display", "block");
    }
  });
  $(".calltop").on("click", function () {
    // clearInterval(tiemr);
    let tiemr = setInterval(function () {
      let $h = $(window).scrollTop();
      $h -= 30;
      $(window).scrollTop($h);
      //   let a;
      //   console.log(a++);
      if ($h <= 0) {
        clearInterval(tiemr);
        $(".calltop").css("display", "none");
      }
    });
  });
});
